package logic

import (
	"fmt"
	"gopkg.in/mgo.v2/bson"
	"math/rand"
	"svvenus/conf"
	"svvenus/vpub/constants"
	"svvenus/znet/zpb"
	"svvenus/znet/ztcp"
	"sync"
)

type PlayerDataGetMode int // 玩家数据获取模式

const (
	PlayerDataGetModeAll    PlayerDataGetMode = 1 // 全部
	PlayerDataGetModeOnline PlayerDataGetMode = 2 // 在线
	//PlayerDataGetModeData   PlayerDataGetMode = 3// 存档
)

var (
	MapPlayer sync.Map // k=pid v=*Player
)

// ConnGetPlayer
// @Description: 从连接获取玩家
// @param conn
// @return *model.Player
func ConnGetPlayer(conn *ztcp.TcpConnection) *Player {
	if conn.BindObj == nil {
		return nil
	}
	return conn.BindObj.(*Player)
}

// ConnHasPlayer
// @Description: 连接是否有玩家
// @param conn
// @return bool
func ConnHasPlayer(conn *ztcp.TcpConnection) bool {
	player := ConnGetPlayer(conn)
	return player != nil
}

// GetRandomName
// @Description: 获取随机名字
// @param gender
// @return string
func GetRandomName(gender int32) string {
	name := fmt.Sprintf("新玩家%d", rand.Intn(999999))
	if len(conf.ListNameSur) > 0 {
		name = conf.ListNameSur[rand.Intn(len(conf.ListNameSur))]
	}
	if gender == conf.EGender男性 {
		if len(conf.ListNameMale) > 0 {
			name += conf.ListNameMale[rand.Intn(len(conf.ListNameMale))]
		}
	} else {
		if len(conf.ListNameFemale) > 0 {
			name += conf.ListNameFemale[rand.Intn(len(conf.ListNameFemale))]
		}
	}
	return name
}

// PlayerCreate
// @Description: 玩家创建
// @param player
// @param req
func PlayerCreate(player *Player, req *zpb.C2GPlayerCreate) {
	player.Id = bson.NewObjectId()
	player.UserId = req.UserId
	player.Name = req.Name
	player.Gender = int(req.Gender)
	player.Head = int(req.Head)

	player.FlagInt = make(map[string]int32)
	player.FlagDailyInt = make(map[string]int32)

	player.Moneys = make(map[string]int64)

	player.MailIds = make([]string, 0)
	player.Mails = make(map[string]*Mail)

	player.Items = make(map[string]int64)
	player.EquipIds = make([]string, 0)
	player.Equips = make(map[string]*Equip)

	{
		ListList := conf.GlobalGetListListInt64(conf.EGlobalId默认货币列表)
		for _, v := range ListList {
			if len(v) >= 2 {
				player.MoneyAdd(int(v[0]), v[1], zpb.RecSource_rsPlayerCreate)
			}
		}
	}
	{
		ListList := conf.GlobalGetListListInt64(conf.EGlobalId默认道具列表)
		for _, v := range ListList {
			if len(v) >= 2 {
				player.ItemAdd(int(v[0]), v[1], zpb.RecSource_rsPlayerCreate)
			}
		}
	}
	{
		List := conf.GlobalGetListInt(conf.EGlobalId默认装备列表)
		for _, v := range List {
			player.EquipAdd(v, zpb.RecSource_rsPlayerCreate)
		}
	}

	player.TimeCreate = constants.SvTime

	player.TimeSave = constants.SvTime
	player.TimeVitalityRecovery = constants.SvTime
}

// PlayerLogin 玩家登录
// @param player
func PlayerLogin(player *Player) {
	player.Online = true

	player.TimeLogin = constants.SvTime
	player.TimeSave = constants.SvTime

	//恢复体力
	player.VitalityRecovery()
	//检查过期邮件
	player.MailCheckOverdue()

	MapPlayer.Store(player.IdString(), player)
}

// PlayerDisconnection 玩家断线
// @param player
func PlayerDisconnection(player *Player) {
	player.Online = false
	player.TimeOffline = constants.SvTime
	player.TimeDisconnection = constants.SvTime

	player.UnsetConn()
}
